package addon.w3.mdx.com;

import java.io.IOException;

import addon.w3.mdx.MDXCOM;
import addon.w3.mdx.LEDataInputStream;
import addon.w3.mdx.MDXException;

/**
	struct {
		long 	nbytesi;
		ASCII 	Name; 			(0x50)
		float	PosX, PosY, PosZ;
		float	FieldOfView;
		float	FarClip;
		float	NearClip;
		struct	{ 			// Target
			float	x, y, z;
			(KCTR)
		}
		(KCRL)
		(KTTR)
	} cameras[ncams];
 * @author madjk
 * not tested;
 */
public class MDXCameraCOM extends MDXCOM {
	int nbytesi = -1;
	String name = null;
	float pos[];
	float fieldOfView;
	float farClip;
	float nearClip;
	
	float targetPos[];
	MDXScaling kctr;
	
	MDXAlpha kcrl;
	MDXScaling kttr;
	
	public String getTag() {
		return "CAMS:camera";
	}
	
	public void _decode(LEDataInputStream ins) throws IOException, MDXException {
		nbytesi = ins.readInt();
		byte temp[] = new byte[80];
		ins.read(temp);
		name = new String(temp);
		pos = new float[3];
		for (int i = 0; i < pos.length; i++) {
			pos[i] = ins.readFloat();
		}
		fieldOfView = ins.readFloat();
		farClip = ins.readFloat();
		nearClip = ins.readFloat();
		targetPos = new float[3];
		for (int i = 0; i < targetPos.length; i++) {
			targetPos[i] = ins.readFloat();
		}
		
		temp = new byte[4];
		ins.read(temp);
		String tag = new String(temp);
		if(tag.equals("KCTR")) {
			kctr = new MDXScaling("KCTR", "KCTR:Translation");
			kctr.decode(ins);
		} else ins.unread(temp);
		
		temp = new byte[4];
		ins.read(temp);
		tag = new String(temp);
		if(tag.equals("KCRL")) {
			kcrl = new MDXAlpha("KCRL", "KCRL:Rotation");
			kcrl.decode(ins);
		} else ins.unread(temp);
		
		temp = new byte[4];
		ins.read(temp);
		tag = new String(temp);
		if(tag.equals("KTTR")) {
			kttr = new MDXScaling("KTTR", "KTTR:Translation");
			kttr.decode(ins);
		} else ins.unread(temp);
	}
	
	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append("TAG : "+getTag());
		sb.append("\n\tILEN : "+nbytesi);
		sb.append("\n\tNAME : "+name);
		sb.append("\n\tPOS : "+pos[0]+" : "+pos[1]+" : "+pos[2]);
		sb.append("\n\tFIELDOFVIEW : "+fieldOfView );
		sb.append("\n\tFARCLIP : "+farClip );
		sb.append("\n\tNEARCLIP : "+nearClip );
		sb.append("\n\tTARGET.POS : "+targetPos[0]+" : "+targetPos[1]+" : "+targetPos[2] );
		sb.append("\n\tTARGET.KCTR : "+(kctr == null ? false : true ));
		sb.append("\n\tKCRL : "+(kcrl == null ? false : true ));
		sb.append("\n\tKTTR : "+(kttr == null ? false : true ));
		return sb.toString();
	}

	@Override
	public boolean isImplicitTag() {
		// TODO Auto-generated method stub
		return false;
	}
}
